<template>
  <j-form-container :disabled="disabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="法人代表" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['legalPerson',validatorRules.legalPerson]" placeholder="请输入法人代表"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idCard', validatorRules.idCard]" placeholder="请输入身份证号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="统一社会信用代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['socialCreditcode']" placeholder="请输入统一社会信用代码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="注册资本(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['registeredCapital']" placeholder="请输入注册资本(万元)" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="注册地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['registeredAddress']" placeholder="请输入注册地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="注册日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择注册日期" v-decorator="['registeredDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="经营范围" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['businessScope']" placeholder="请输入经营范围"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
  </j-form-container>
</template>
<script>
import pick from 'lodash.pick'
import { getAction } from '@/api/manage'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'

export default {
  name: 'XyParkOwnerbusinessForm',
  components: {
    JFormContainer,
    JDate
  },
  props: {
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      validatorRules: {
        idCard: {
          rules: [
            { required: true, message: '请输入身份证号' }
          ]
        },
        legalPerson: {
          rules: [
            { required: true, message: '请输入法人' }
          ]
        }
      },
      confirmLoading: false
    }
  },
  methods: {
    initFormData(url, ownerCode) {
      this.clearFormData()
      if (!ownerCode) {
        this.edit({})
      } else {
        getAction(url, { ownerCode: ownerCode }).then(res => {
          if (res.success) {
            const records = res.result
            if (records && records.length > 0) {
              this.edit(records[0])
            }
          }
        })
      }
    },
    edit(record) {
      this.model = Object.assign({}, record)
      const fieldval = pick(this.model, 'legalPerson', 'idCard', 'socialCreditcode', 'registeredCapital', 'registeredAddress', 'registeredDate', 'businessScope')
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
    },
    getFormData() {
      const formdata_arr = []
      this.form.validateFields((err, values) => {
        if (!err) {
          const formdata = Object.assign(this.model, values)
          let isNullObj = true
          Object.keys(formdata).forEach(key => {
            if (formdata[key]) {
              isNullObj = false
            }
          })
          if (!isNullObj) {
            formdata_arr.push(formdata)
          }
        } else {
          this.$emit('validateError', '业主工商信息表单校验未通过')
        }
      })
      return formdata_arr
    },
    validateFields() {
      return new Promise((resolve, reject) => {
        // 验证主表表单
        this.form.validateFields((err, values) => {
          err ? reject({ error: err }) : resolve(values)
        })
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'legalPerson', 'idCard', 'socialCreditcode', 'registeredCapital', 'registeredAddress', 'registeredDate', 'businessScope'))
    },
    clearFormData() {
      this.form.resetFields()
      this.model = {}
    }
  }
}
</script>
